package com.mengxinya.ys.spider.zhihu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mengxinya.ys.curldriver.CurlPageWalker;
import com.mengxinya.ys.curldriver.RequestCurlDriverHandler;
import com.mengxinya.ys.curldriver.RequestCurlDriverTemplateHandler;
import com.mengxinya.ys.grap.GrapEngine;
import com.mengxinya.ys.grap.GrapEngineBuilder;

import java.util.Map;

public class ZhihuSpider {
    public void start() {
        RequestCurlDriverHandler handler = RequestCurlDriverHandler.makeGet(
                (driver, response) -> {
                    JSONObject data = JSON.parseObject(response.body());
                    System.out.println(data);
                },
                "https://www.zhihu.com/api/v4/members/hasmart/answers?include=data[*].is_normal,admin_closed_comment,reward_info,is_collapsed,annotation_action,annotation_detail,collapse_reason,collapsed_by,suggest_edit,comment_count,can_comment,content,editable_content,attachment,voteup_count,reshipment_settings,comment_permission,mark_infos,created_time,updated_time,review_info,excerpt,paid_info,reaction_instruction,is_labeled,label_info,relationship.is_authorized,voting,is_author,is_thanked,is_nothelp,is_recognized;data[*].vessay_info;data[*].author.badge[?(type=best_answerer)].topics;data[*].author.vip_info;data[*].question.has_publishing_draft,relationship&offset=60&limit=20&sort_by=created"
        );
        handler = RequestCurlDriverHandler.withCookies(handler, "_zap=f9bde19c-b7ee-44f0-8455-3bd4ccdaca2a; d_c0=\"ACDQzxaGshSPTvIRVL7fJ10_vAGYr4UqMZ8=|1648357912\"; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1681013821,1681087883,1681096965,1681262634; q_c1=4c37105e4e1947c69567fb8707d0bb91|1650709012000|1650709012000; __snaker__id=T0gLF3ZLw1AaF0x4; _9755xjdesxxd_=32; YD00517437729195%3AWM_NI=jFjqj1zkzW0WR6D24tpRUPGS2uomHUbvfW%2BLzIRSwzXh2NmbuhFqJjhSa%2BljHF8gmbRY8Kn9OkYXtpaqIYIQi4rx5Mcqa6koGf7grb%2FK7M5jHdQXCKb3Axk4TIOGtrUDc1o%3D; YD00517437729195%3AWM_NIKE=9ca17ae2e6ffcda17…NKMUhsblQyQjBEVUdFd2g4czVEY0RBPT0=|af4c67b8f62e6de7b4c3a989523df7916ee9bb887288abb72daec50223067fb8; _xsrf=7b33a8e1-2f77-478a-a290-f81a9a8b4c4d; KLBRSID=e42bab774ac0012482937540873c03cf|1681263694|1681262630; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1681263107; tst=f; SESSIONID=mlh3cYNIm664lrJmjBGmoSgwRogQxu3UMv7k8wn1V17; JOID=VV0dCkuZ9TOlgZenZZfgozCqHxB-9pRan-XM5C3zwQiQ5OLvBbzxDc-EmKttJU-k1IczlEDV52QouAqQJqoOFMo=; osd=UVscAUyd8zKuhpOhZJznpzarFBd68JVRmOHK5Sb0xQ6R7-XrA736CsuCmaBqIUml34A3kkHe4GAuuQGXIqwPH80=");
        handler = RequestCurlDriverHandler.defaultHeader(handler);
        handler = RequestCurlDriverHandler.withHeaders(handler, Map.of(
                "x-ab-param", "",
                "x-ab-pb", "CkYbAD8ARwC0AGkBagF0ATsCzALXAtgCtwPWBFEFiwWMBZ4FMQbrBicHdAh5CGAJ9AlrCr4KcQuHC+AL5QvmC3EMjwzDDPgMEiMHAAAAAQAAAQAAAAAEAQAAAQEAAAAGAgMAAAAAAAAABAACAA==",
                "x-zse-93", "101_3_3.0",
                "x-zse-96", "2.0_26l0C2pbIJx0O4Ts096NiAHqxJHKaXloI51B1+WYWVmKLMBbgQw6T27es6/uqkBA",
                "x-zst-81", "3_2.0VhnTj77m-qofgh3TxTP0EA90r4nL-BOB8gN97Au0rRFxrRF0w4S1Fve97HnxEHt0miFGkDQye8FL7AtqM6O1VDQyQ6nxrRPCHukMoCXBEgOsiRP0XL2ZUBXmDDV9qhnyTXFMnXcTF_ntRueThiHYobULi9x_wG39KMNqrTN9ObOKZBOG7CFYACe_8u3K6w2BnvS0jhOq_GLPv7YOlqe91gO9YqoMbToMErHLcrH1JCoXVh30G4w06LOB2Ax8BrLMvBLGwvgBeHSp_9c0qvNCkcNqcMFLkutOMqF1oRL_XwNKTbN1mTVfPU91cCc18wXm5qfz60oLTcPMtCe_figKkgw9_BLBFDOmsUtCLUrqsJOO_wc0uDe8NwNCxComSwO_tCtLX9X1JBwOpcPGTg3MQTVGbhOpawXCUvHfEuL0SqN_rHOG5heLe8LC"
                ));
        GrapEngine grapEngine = GrapEngineBuilder.makeInstance(handler);

        grapEngine.start();
    }

    public void start1() {
        CurlPageWalker walker = CurlPageWalker.make(3, 3);

        RequestCurlDriverTemplateHandler templateHandler = RequestCurlDriverTemplateHandler.makeGet((driver, response) -> {
            JSONObject jsonObject = JSON.parseObject(response.body());
            System.out.println(jsonObject);
        }, "https://www.zhihu.com/api/v4/web_moments/hasmart/activities?offset=1679133777364&page_num=${page}");
        RequestCurlDriverHandler handler = RequestCurlDriverHandler.wrapPage(templateHandler, walker);

        handler = RequestCurlDriverHandler.withCookies(handler, "_zap=f9bde19c-b7ee-44f0-8455-3bd4ccdaca2a; d_c0=\"ACDQzxaGshSPTvIRVL7fJ10_vAGYr4UqMZ8=|1648357912\"; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1680919486,1681013821,1681087883,1681096965; q_c1=4c37105e4e1947c69567fb8707d0bb91|1650709012000|1650709012000; __snaker__id=T0gLF3ZLw1AaF0x4; _9755xjdesxxd_=32; YD00517437729195%3AWM_NI=jFjqj1zkzW0WR6D24tpRUPGS2uomHUbvfW%2BLzIRSwzXh2NmbuhFqJjhSa%2BljHF8gmbRY8Kn9OkYXtpaqIYIQi4rx5Mcqa6koGf7grb%2FK7M5jHdQXCKb3Axk4TIOGtrUDc1o%3D; YD00517437729195%3AWM_NIKE=9ca17ae2e6ffcda17…NKMUhsblQyQjBEVUdFd2g4czVEY0RBPT0=|af4c67b8f62e6de7b4c3a989523df7916ee9bb887288abb72daec50223067fb8; _xsrf=7200717d-6ecb-4f1e-9f0a-a732c08e25b9; KLBRSID=d6f775bb0765885473b0cba3a5fa9c12|1681097574|1681096963; tst=f; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1681096969; SESSIONID=0idw2oQlyrx81U4VGv8Sldh3OEcfAERclMYVHDRDY9j; JOID=VlwSA08AQelHx7uwdwRRddrnOQJgMCuKNPXU4hx5M5J48u6EIgTE2SfHuLR0PJi3nwoMIJh9sEK2pLAaikHqRRI=; osd=U18UBUgFQu9BwL6zcQJWcNnhPwVlMy2MM_DX5Bp-NpF-9OmBIQLC3iLEvrJzOZuxmQ0JI557t0e1orYdj0LsQxU=");
        handler = RequestCurlDriverHandler.defaultHeader(handler);
        handler = RequestCurlDriverHandler.withHeaders(handler, Map.of(
                "x-ab-param", "",
                "x-ab-pb", "CkYbAD8ARwC0AGkBagF0ATsCzALXAtgCtwPWBFEFiwWMBZ4FMQbrBicHdAh5CGAJ9AlrCr4KcQuHC+AL5QvmC3EMjwzDDPgMEiMHAAAAAQAAAQAAAAAEAQAAAQEAAAAGAgMAAAAAAAAABAACAA==",
                "x-api-version", "3.0.40",
                "x-zse-93", "101_3_3.0",
                "x-zse-96", "2.0_bURvFpLA1H075oLT5qseb8i6FWmz3VTPFXGZr=FwcFY/jjpPntQ1tqoc=c=NhaQu",
                "x-zst-81", "3_2.0VhnTj77m-qofgh3TxTP0EA90r4nL-BOB8gN97Au0rRFxrRF0w4S1Fve97HnxEHt0miFGkDQye8FL7AtqM6O1VDQyQ6nxrRPCHukMoCXBEgOsiRP0XL2ZUBXmDDV9qhnyTXFMnXcTF_ntRueTh_COoMLCt9NXeUOM6JH8FcL1fgCxEu2mW9Lm-bXqJqpfo4S8UqxKoLOOECLPvASYf9FqrRgLQTNYWuCq0CoByheYj9wBBcfm1wVfUuoB3hYVkHcfvGwfXbHM6G2YS0g0GTe88ULYUwL_60N_KXL_IULOI93BDvH07DLCsbpOju21VGO04qfze4eGcXN9jGcBN9SM0uOBkJxBaugGEcHOgUuMeqO81vS1JU3KFwOC8hNsFw3L8COypGeqIgw9UDLBWcOyuUVqPGxMnBxBYQLMZcOfqwCGfGgGuhXOuBHC"
        ));

        GrapEngine grapEngine = GrapEngineBuilder.makeInstance(handler);

        grapEngine.start();
    }
}
